import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import path from 'path'
import viteCompression from 'vite-plugin-compression'
import styleImport from 'vite-plugin-style-import'
import { ViteEjsPlugin } from 'vite-plugin-ejs'
import { viteCommonjs } from '@originjs/vite-plugin-commonjs'
import AutoImport from 'unplugin-auto-import/vite'
import Components from 'unplugin-vue-components/vite'
import { ElementPlusResolver } from 'unplugin-vue-components/resolvers'

const resolve = (...data: string[]) => path.resolve(__dirname, ...data)
// https://vitejs.dev/config/
export default defineConfig({
  base: './',
  build: {
    commonjsOptions: {
      transformMixedEsModules: true,
    },
  },
  plugins: [
    viteCommonjs(),
    vue(),
    ViteEjsPlugin((config) => {
      return {
        ...config.define,
        from: 'vite',
        title: '智言AI',
      }
    }),
    viteCompression({
      verbose: true,
      disable: false,
      threshold: 10240,
      algorithm: 'gzip',
      ext: '.gz',
    }),
    styleImport({
      libs: [
        {
          libraryName: 'element-plus',
          esModule: true,
          ensureStyleFile: true,
          resolveStyle: (name) => {
            name = name.slice(3)
            return `element-plus/theme-chalk/src/${name}.scss`
          },
        },
      ],
    }),
    // ...
    AutoImport({
      resolvers: [ElementPlusResolver()],
    }),
    Components({
      resolvers: [ElementPlusResolver()],
    }),
  ],
  resolve: {
    alias: {
      "@": path.resolve(__dirname, "src"),
      '~data': resolve('src/assets/data'),
      emitter: require.resolve('emitter-component'),
    },
  },
  css: {
    preprocessorOptions: {
      less: {
        modifyVars: {
          color: `true; @import "./src/assets/styles/color.less";`,
        },
      },
    },
  },
  define: {
    'process.env': process.env,
  },
  server: {
    hmr: { overlay: false },
    port: 4001,
    // host: '172.16.83.12',
    // proxy: {
    //   '/api': {
    //     target: 'https://rmt-design-dev.imp360.cn/api/',
    //     changeOrigin: true,
    //     rewrite: (path) => path.replace(/^\/api/, ''),
    //   },
    // },
  },
})
